# coding: utf-8

from PySide6.QtCore import Qt
from PySide6.QtWidgets import QHBoxLayout
from qfluentwidgets import BodyLabel, IconWidget, setFont, TransparentPushButton
from qframelesswindow import TitleBarBase
from qframelesswindow.utils import toggleMaxState

from common import Icon, isWin11
from .title_btn import TitleBarButton, MaximizeButton

if isWin11():
    from qframelesswindow import AcrylicWindow as Window
else:
    from qframelesswindow import FramelessWindow as Window


class VideoTitleBar(TitleBarBase):
    def __init__(self, parent):
        super().__init__(parent)
        self.__delButton()
        self.horizontalLayout = QHBoxLayout(self)
        self.homeBtn = TransparentPushButton(Icon.HOME, '返回主页', self)
        self.iconWidget = IconWidget(self)
        self.titleLabel = BodyLabel(self)
        self.topShowBtn = TitleBarButton(Icon.TOP_SHOW, self)
        self.minBtn = TitleBarButton(Icon.MIN, self)
        self.maxBtn = MaximizeButton(Icon.MAX, self)
        self.closeBtn = TitleBarButton(Icon.CLOSE, self)

        self.__initWidgets()
        self.__connectSignals()

    def __delButton(self):
        self.minBtn.hide()
        self.closeBtn.hide()
        self.maxBtn.hide()
        self.maxBtn.deleteLater()
        self.minBtn.deleteLater()
        self.closeBtn.deleteLater()

    def __initWidgets(self):
        self.setFixedHeight(50)
        setFont(self.titleLabel, 16)
        self.homeBtn.setFixedSize(110, 36)
        self.iconWidget.setFixedSize(30, 30)
        self.titleLabel.setAlignment(Qt.AlignmentFlag.AlignCenter)
        # self.titleLabel.setTextColor(QColor(255, 255, 255), QColor(255, 255, 255))

        self.horizontalLayout.addWidget(self.homeBtn, 0, Qt.AlignmentFlag.AlignLeft)
        self.horizontalLayout.addStretch(1)
        self.horizontalLayout.addWidget(self.iconWidget, 0, Qt.AlignmentFlag.AlignCenter)
        self.horizontalLayout.addWidget(self.titleLabel, 0, Qt.AlignmentFlag.AlignCenter)
        self.horizontalLayout.addStretch(1)
        self.horizontalLayout.addWidget(self.topShowBtn, 0, Qt.AlignmentFlag.AlignRight)
        self.horizontalLayout.addWidget(self.minBtn, 0, Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignRight)
        self.horizontalLayout.addWidget(self.maxBtn, 0, Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignRight)
        self.horizontalLayout.addWidget(self.closeBtn, 0, Qt.AlignmentFlag.AlignVCenter | Qt.AlignmentFlag.AlignRight)

    def __connectSignals(self):
        self.minBtn.clicked.connect(self.window().showMinimized)
        self.maxBtn.clicked.connect(self.__toggleMaxState)
        self.closeBtn.clicked.connect(self.window().close)
        self.window().windowTitleChanged.connect(self.titleLabel.setText)
        self.window().windowIconChanged.connect(self.iconWidget.setIcon)
        self.topShowBtn.clicked.connect(self._toggleTopShow)

    def __toggleMaxState(self):
        """ 切换窗口和更改图标的最大化状态 """
        toggleMaxState(self.window())

    def _toggleTopShow(self):
        """ 切换窗口的顶部显示状态 """
        window: Window = self.window()
        windowFlags = window.windowFlags()
        if windowFlags & Qt.WindowType.WindowStaysOnTopHint:
            window.setWindowFlag(Qt.WindowType.WindowStaysOnTopHint, False)
            self.topShowBtn.setIcon(Icon.TOP_SHOW)
        else:
            window.setWindowFlag(Qt.WindowType.WindowStaysOnTopHint, True)
            self.topShowBtn.setIcon(Icon.TOP_SHOW_FULL)
        window.updateFrameless()
        window.show()
